﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Text;
using System.Web.SessionState;

namespace lcgps.Web.gpsinfo
{
    /// <summary>
    /// GetGPS 的摘要说明
    /// </summary>
    public class GetGPS : IHttpHandler, IRequiresSessionState
    {
        lcgps.BLL.gpsinfo bll = new lcgps.BLL.gpsinfo();
        StringBuilder result = new StringBuilder();
        StringBuilder sb = new StringBuilder();
        public void ProcessRequest(HttpContext context)
        {
            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }
            if (ac == "queryt")
                Querytree(context);
            else if (ac == "road")
            {
                Query(context);
            }
            else if (ac == "delete")
            {
                delete(context);
            }
            else if (ac == "queryone")
            {
                Queryone(context);
            }
            else if (ac == "querym")
            {
                Querym(context);
            }
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                int aid = 0;
                string pid = "", pcid = "", mid = "";
                bool ishave = false;
                //获取前台传来的值 
                if (null != context.Request.QueryString["pid"])
                {//获取前台传来的值  
                    pid = context.Request.QueryString["pid"].ToString().Trim();
                    ishave = true;
                }
                if (null != context.Request.QueryString["pcid"])
                {//获取前台传来的值  
                    pcid = context.Request.QueryString["pcid"].ToString().Trim();
                    ishave = true;
                }
                if (null != context.Request.QueryString["mid"])
                {
                    mid = context.Request.QueryString["mid"].ToString().Trim();
                    ishave = true;
                }
                if (ishave)
                {
                    StringBuilder strWhere = new StringBuilder();
                    if (pid != "")
                    {
                        strWhere.AppendFormat(" and proid= '{0}'", pid);
                    }
                    if (pcid != "")
                    {
                        strWhere.AppendFormat(" and purchaseid= '{0}'", pcid);
                    }
                    if (mid != "")
                    {
                        strWhere.AppendFormat(" and mid= {0}", mid);
                    }

                    if (bll.Delete(strWhere.ToString()))
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();
                    }
                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void Querym(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {                
                string pid = "", pcid = "", mid = "";
                //获取前台传来的值 
                if (null != context.Request.Form["pid"])
                {//获取前台传来的值  
                    pid = context.Request.Form["pid"].ToString().Trim();

                }
                if (null != context.Request.Form["pcid"])
                {//获取前台传来的值  
                    pcid = context.Request.Form["pcid"].ToString().Trim();
                }
                if (null != context.Request.Form["mid"])
                {
                    mid = context.Request.Form["mid"].ToString().Trim();
                }
                string db = "";
                db = System.Configuration.ConfigurationManager.AppSettings["db"];
                DataSet ds = bll.GetListMatrial(pid,pcid,mid,db);
                if (ds != null && ds.Tables[0].Rows.Count > 0)
                {
                    int count = ds.Tables[0].Rows.Count;//获取条数  
                    string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                    context.Response.Write(strJson);//返回给前台页面 
                }
                else
                {
                    context.Response.Write("{\"total\":0,\"rows\":[]}");                    
                    context.Response.End();
                }
            }
            context.Response.End();


        }
        public void Queryone(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {
                string StartTime = "", EndTime = "";                
                if (null != context.Request.Form["StartTime"])
                {//获取前台传来的值  
                    StartTime = context.Request.Form["StartTime"].ToString().Trim();
                }
                if (null != context.Request.Form["EndTime"])
                {//获取前台传来的值  
                    EndTime = context.Request.Form["EndTime"].ToString().Trim();
                }
               string  pid = "",pcid="",mid="";
               //获取前台传来的值 
               if (null != context.Request.Form["pid"])
               {//获取前台传来的值  
                   pid = context.Request.Form["pid"].ToString().Trim();
                   
               }
               if (null != context.Request.Form["pcid"])
               {//获取前台传来的值  
                   pcid = context.Request.Form["pcid"].ToString().Trim();
                   
               }
               if (null != context.Request.Form["mid"])
               {
                   mid = context.Request.Form["mid"].ToString().Trim();
                   
               }
                int pageRows, page;
                pageRows = 10;
                page = 1;
                string order, sort, oderby; order = sort = oderby = "";
                if (null != context.Request.Form["rows"])
                {
                    pageRows = int.Parse(context.Request.Form["rows"].ToString().Trim());
                }
                if (null != context.Request.Form["page"])
                {
                    page = int.Parse(context.Request.Form["page"].ToString().Trim());
                }
                if (null != context.Request.Form["sort"])
                {
                    order = context.Request.Form["sort"].ToString().Trim();
                }
                if (null != context.Request.Form["order"])
                {
                    sort = context.Request.Form["order"].ToString().Trim();
                }                
                    StringBuilder strWhere = new StringBuilder();
                    if (pid != "")
                    {
                        strWhere.AppendFormat(" and proid= '{0}'", pid);
                    }
                    if (pcid != "")
                    {
                        strWhere.AppendFormat(" and purchaseid= '{0}'", pcid);
                    }
                    if (mid != "")
                    {
                        strWhere.AppendFormat(" and mid= {0}", mid);
                    }


                    if (StartTime != "")
                    {
                        strWhere.AppendFormat(" gdatetime>='{0}' and ", StartTime);
                    }
                    if (EndTime != "")
                    {
                        strWhere.AppendFormat(" gdatetime<='{0}' and ", EndTime);
                    }
                    int startindex = strWhere.ToString().LastIndexOf("and");//获取最后一个and的位置  
                    if (startindex >= 0)
                    {
                        strWhere.Remove(startindex, 3);//删除多余的and关键字  
                    }                 
                    if (sort != "" && order != "")
                    {
                        oderby = order + " " + sort;
                    }
                    DataSet ds = bll.GetListByPage(strWhere.ToString(), oderby, (page - 1) * pageRows + 1, page * pageRows);
                    int count = bll.GetRecordCount(strWhere.ToString());//获取条数  
                    string strJson = ToJson.Dataset2Json(ds, count);//DataSet数据转化为Json数据  
                    context.Response.Write(strJson);//返回给前台页面 
                }
                context.Response.End();
            

        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                
                string  pid = "",pcid="",mid="";
                bool ishave = false;            
                //获取前台传来的值 
                if (null != context.Request.QueryString["pid"])
                {//获取前台传来的值  
                    pid = context.Request.QueryString["pid"].ToString().Trim();
                    ishave = true;
                }
                if (null != context.Request.QueryString["pcid"])
                {//获取前台传来的值  
                    pcid = context.Request.QueryString["pcid"].ToString().Trim();
                    ishave = true;
                }
                if (null != context.Request.QueryString["mid"])
                {
                    mid = context.Request.QueryString["mid"].ToString().Trim();
                    ishave = true;
                }

                if (ishave)
                {
                    StringBuilder strWhere = new StringBuilder();
                    if (pid != "")
                    {
                        strWhere.AppendFormat(" and proid= '{0}'", pid);
                    }
                    if (pcid != "")
                    {
                        strWhere.AppendFormat(" and purchaseid= '{0}'", pcid);
                    }
                    if (mid != "")
                    {
                        strWhere.AppendFormat(" and mid= {0}", mid);
                    }  
                    DataSet ds = bll.GetList(strWhere.ToString());                   
                    string strJson = ToJson.Dataset2Json(ds,ds.Tables[0].Rows.Count);//DataSet数据转化为Json数据  
                    context.Response.Write(strJson);//返回给前台页面 
                }
                context.Response.End();
            }

        }
        private string GetTreeJsonByTableOne(DataTable tabel, string idCol, string txtCol, string bmCol)
        {
            result.Remove(0, result.Length);
            if (tabel.Rows.Count > 0)
            {
                result.Append("[");

                foreach (DataRow row in tabel.Rows)
                {
                    result.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"mid\":\"" + row[bmCol] + "\",\"pcid\":\"" + row[txtCol] + "\"},");
                }
                result = result.Remove(result.Length - 1, 1);

                result.Append("]");

            }
            return result.ToString();
        }
        public void Querytree(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {               
                
                DataSet ds = bll.GetListT("");
                result.Remove(0, result.Length);
                string str = GetTreeJsonByTableOne(ds.Tables[0], "proid", "purchaseid", "mid");//proid,purchaseid,mid
                context.Response.Write(str);//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}